#!/bin/bash

APP_NAME=$(basename $1)
JAR_NAME=${APP_NAME}.jar
TEMP_NAME=${JAR_NAME}-temp
PEM_PATH=/d/prod1.pem

echo APP_NAME: ${APP_NAME}
echo JAR_NAME: ${JAR_NAME}
echo TEMP_NAME: ${TEMP_NAME}

echo "------Back Application("${APP_NAME}")------"
#ssh-add -k "${PEM_PATH}"
ssh root@47.97.157.158 \
"mkdir -p /home/admin/apps-jar/"${APP_NAME}" ;
cd /home/admin/apps-jar/"${APP_NAME}" ;
mv "$JAR_NAME" "$TEMP_NAME" ;
mv "$JAR_NAME"-backup "$JAR_NAME" ;
mv "$TEMP_NAME" "$JAR_NAME"-backup ;
cd ../ ;
sh app-start.sh "${APP_NAME}"/"${JAR_NAME}" >/dev/null 2>&1 ;
tailf -n 200 "${APP_NAME}"/~/swift/${APP_NAME%-exec}/logs/info.log"
